import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/welcome',
      name: 'welcome',
      component: () => import('../views/WelcomeView.vue'),
      meta: { requiresAuth: false, allowVisitor: true },
    },
    {
      path: '/visitor-map',
      name: 'visitor-map',
      component: () => import('../views/VisitorMapView.vue'),
      meta: { requiresAuth: false, allowVisitor: true },
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: { requiresAuth: false, allowVisitor: true },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
      meta: { requiresAuth: false, allowVisitor: true },
    },
    {
      path: '/',
      component: () => import('../views/LayoutView.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: '',
          name: 'home',
          component: () => import('../views/HomeView.vue'),
          meta: { requiresAuth: true },
        },
        {
          path: 'admin',
          name: 'admin',
          component: () => import('../views/AdminView.vue'),
          meta: { requiresAuth: true, role: 'admin' },
        },
        {
          path: 'maintenance',
          name: 'maintenance',
          component: () => import('../views/MaintainerView.vue'),
          meta: { requiresAuth: true, role: 'maintainer' },
        },
        {
          path: 'user',
          name: 'user',
          component: () => import('../views/UserView.vue'),
          meta: { requiresAuth: true, role: 'user' },
        },
        {
          path: 'user/report-damage',
          name: 'user-report-damage',
          component: () => import('../views/UserMapView.vue'),
          meta: { requiresAuth: true, role: 'user' },
        },
        {
          path: 'admin/road-damage',
          name: 'admin-road-damage',
          component: () => import('../views/AdminRoadDamageView.vue'),
          meta: { requiresAuth: true, role: 'admin' },
        },
        {
          path: 'maintenance/road-damage',
          name: 'maintenance-road-damage',
          component: () => import('../views/MaintainerRoadDamageView.vue'),
          meta: { requiresAuth: true, role: 'maintainer' },
        },
        {
          path: 'maintenance/reports',
          name: 'maintenance-reports',
          component: () => import('../views/MaintainerReportsView.vue'),
          meta: { requiresAuth: true, role: 'maintainer' },
        },
      ],
    },
    // 将未匹配的路由重定向到欢迎页
    {
      path: '/:pathMatch(.*)*',
      redirect: '/welcome',
    },
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const requiresAuth = to.meta.requiresAuth as boolean
  const requiredRole = to.meta.role as string | undefined
  const allowVisitor = to.meta.allowVisitor as boolean

  // 如果是访客模式且该页面允许访客访问
  if (userStore.isVisitor && allowVisitor) {
    next()
    return
  }

  // 如果是访客模式但该页面不允许访客访问
  if (userStore.isVisitor && !allowVisitor) {
    next('/welcome')
    return
  }

  // 如果页面需要认证
  if (requiresAuth) {
    // 如果用户未登录，重定向到登录页
    if (!userStore.isLoggedIn) {
      next('/login')
      return
    }

    // 如果页面需要特定角色且用户角色不匹配，重定向到首页
    if (requiredRole && userStore.userInfo.role !== requiredRole) {
      // 根据用户角色重定向到相应页面
      if (userStore.userInfo.role === 'admin') {
        next('/admin')
      } else if (userStore.userInfo.role === 'maintainer') {
        next('/maintenance')
      } else if (userStore.userInfo.role === 'user') {
        next('/user')
      } else {
        next('/')
      }
      return
    }
  } else {
    // 如果用户已登录且尝试访问登录页或注册页，重定向到首页
    if (
      (to.path === '/login' || to.path === '/register' || to.path === '/welcome') &&
      userStore.isLoggedIn
    ) {
      // 根据用户角色重定向到相应页面
      if (userStore.userInfo.role === 'admin') {
        next('/admin')
      } else if (userStore.userInfo.role === 'maintainer') {
        next('/maintenance')
      } else if (userStore.userInfo.role === 'user') {
        next('/user')
      } else {
        next('/')
      }
      return
    }
  }

  next()
})

export default router
